home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / bgipaint.cpp < prev    next >
C/C++ Source or Header  |  1994-11-20  |  13KB  |  479 lines

  1. #include <stdlib.h>     // Demo only !!!
  2. #include "bgipaint.h"
  3. color_to_16 tricolors[16] =
  4.     {   { BLACK, 0 },
  5.         { BLUE, RGB(0, 0, 128) },
  6.         { GREEN, RGB(0, 128, 0) },
  7.         {  CYAN, RGB(0, 128, 128) },
  8.         {  RED, RGB(128, 0, 0) },
  9.         { MAGENTA, RGB(74, 23, 74) },
  10.         { BROWN, RGB(128, 64, 64) },
  11.         { LIGHTGRAY, RGB(128, 128, 128) },
  12.         { DARKGRAY, RGB(64, 64, 64) },
  13.         { LIGHTBLUE, RGB(0, 0, 255) },
  14.         { LIGHTGREEN, RGB(0, 255, 0) },
  15.         { LIGHTCYAN, RGB(0, 255, 255) },
  16.         { LIGHTRED, RGB(255, 0, 0) },
  17.         { LIGHTMAGENTA, RGB(255, 0, 255) },
  18.         { YELLOW, RGB(255,255,30) },
  19.         { WHITE, RGB(255, 255, 255) }
  20.     };
  21.  
  22.  
  23. int To_Paint::getcolor(COLORREF color)
  24.     {
  25.     int col = 0;
  26.     COLORREF l;
  27.     int a = 0;
  28.     if(tricolors[0].colorref > color)
  29.         {
  30.         l = tricolors[0].colorref - color;
  31.         a = 1;
  32.         }
  33.     else
  34.         l = color - tricolors[0].colorref;
  35.     for(int i = 0; i < 16; i++)
  36.         {
  37.         if(a)
  38.             {
  39.             if(l > tricolors[i].colorref - color)
  40.                 {
  41.                 col = i;
  42.                 l = tricolors[i].colorref - color;
  43.                 }
  44.             }
  45.         else
  46.             if(l > color - tricolors[i].colorref)
  47.                 {
  48.                 col = i;
  49.                 l = color - tricolors[i].colorref;
  50.                 }
  51.         }
  52.     return col;
  53.     }
  54. ////////////////////
  55. #ifdef WIN_GDI
  56.  
  57. void To_Paint::lineto(int x, int y)
  58.     {
  59.     HPEN pen = CreatePen(PenStyle, PenSize, color);
  60.     HPEN old_pen;
  61.     (HGDIOBJ)old_pen = SelectObject(DC, pen);
  62.     ::LineTo(DC, x, y);
  63.     SelectObject(DC, old_pen);
  64.     DeleteObject(pen);
  65.     }
  66. /////////////////////
  67. void To_Paint::fillpoly(int numpoints, int* polypoints)
  68.     {
  69.     HPEN pen = CreatePen(PenStyle, PenSize, color);
  70.     HPEN old_pen;
  71.     (HGDIOBJ)old_pen = SelectObject(DC, pen);
  72.     HBRUSH brush;
  73.     if(pattern_num == SOLID_FILL)
  74.         brush = CreateSolidBrush(BrushColor);
  75.     else
  76.         brush = CreateHatchBrush(pattern_num, BrushColor);
  77.     HBRUSH old_brush;
  78.     (HGDIOBJ)old_brush = SelectObject(DC, brush);
  79.     ::Polygon(DC, (POINT*)polypoints, numpoints);
  80.     SelectObject(DC, old_brush);
  81.     DeleteObject(brush);
  82.     SelectObject(DC, old_pen);
  83.     DeleteObject(pen);
  84.     }
  85. //////////////////////
  86. void To_Paint::drawpoly(int numpoints, int far* points)
  87.     {
  88.     HPEN pen = CreatePen(PenStyle, PenSize, color);
  89.     HPEN old_pen;
  90.     (HGDIOBJ)old_pen = SelectObject(DC, pen);
  91.     ::Polyline(DC, (POINT*)points, numpoints);
  92.     SelectObject(DC, old_pen);
  93.     DeleteObject(pen);
  94.     }
  95.  
  96. #endif WIN_GDI
  97. //////////////////////////////////////////////////////
  98.  
  99. KH_Paint::KH_Paint()
  100.     : To_Paint(),
  101.       Paint()
  102.       {
  103.       }
  104.  
  105. int KH_Paint::getx()
  106.     {
  107.     int x = To_Paint::getx();
  108.     loc pos(transform(x, 0));
  109.     return pos.X;
  110.     }
  111. //////////////////////////
  112. int KH_Paint::gety()
  113.     {
  114.     int y = To_Paint::gety();
  115.     loc pos(transform(0, y));
  116.     return pos.Y;
  117.     }
  118. //////////////////////////
  119. void KH_Paint::putpixel(int x, int y)
  120.     {
  121.     loc pos(transform(x, y));
  122.     To_Paint::putpixel(pos.X, pos.Y);
  123.     }
  124. //////////////////
  125. void KH_Paint::lineto(int x, int y)
  126.     {
  127.     loc to(transform(x, y));
  128.     To_Paint::lineto(to.X, to.Y);
  129.     }
  130. ///////////////////
  131. void KH_Paint::moveto(int x, int y)
  132.     {
  133.     loc to(transform(x, y));
  134.     To_Paint::moveto(to.X, to.Y);
  135.     }
  136. ///////////////////
  137. void KH_Paint::ellipse(int x, int y, int stangle, int endangle, int xr, int yr)
  138.     {
  139.     if(xr <= 0 || yr <= 0)
  140.         return;
  141.     loc pos;
  142.     pos = loc(x,y);
  143.     long x1 = xr;
  144.     long y1 = yr;
  145.     moveto(pos.X + x1 * cos(stangle) / 1000,
  146.            pos.Y - y1 * sin(stangle) / 1000);
  147.  
  148.     if(fill == OFF)
  149.         for(int i = stangle; i <= endangle; i++)
  150.             lineto(pos.X + x1 * cos(i) / 1000,
  151.                    pos.Y - y1 * sin(i) / 1000);
  152.     else
  153.         {
  154.         int points[730];                   // 360 * 2
  155.         for(int i = stangle, j = 0; i < endangle; i++,j++)
  156.             {
  157.             points[2 * j] = (int)(pos.X + x1 * cos(i) / 1000);
  158.             points[2 * j + 1] = (int)(pos.Y - y1 * sin(i) / 1000);
  159.             }
  160.         if((endangle - stangle) % 360)
  161.             {
  162.             points[2 * j] = pos.X;
  163.             points[2 * j + 1] = pos.Y; j++;
  164.  
  165.             points[2 * j] = points[0];
  166.             points[2 * j + 1] = points[1];
  167.             }
  168.         fillpoly(j, points);
  169.         }
  170.     }
  171. ///////////////////
  172. void KH_Paint::drawpoly(int numpoints, int far* points)
  173.     {
  174.     for(int i = 0; i < numpoints * 2; i += 2)
  175.         {
  176.         loc tmp = transform(points[i], points[i + 1]);
  177.         points[i] = tmp.X;
  178.         points[i + 1] = tmp.Y;
  179.         }
  180.     if(fill == OFF)
  181.         To_Paint::drawpoly(numpoints, points);
  182.     else
  183.         To_Paint::fillpoly(numpoints, points);
  184.     }
  185.  
  186. ///////////////////////////// End of DOS BGI part ///////////////////////////
  187. void KH_Paint::rectangle(int x1, int y1, int x2, int y2)
  188.     {
  189.     int p[] =  { x1, y1, x2, y1, x2, y2, x1, y2, x1, y1 };
  190.         drawpoly(5, p);
  191.     }
  192. ///////////////////
  193. void KH_Paint::outtext(uchar far* str, int dir)
  194.     {
  195.     outtextxy(getx(), gety(), str, dir);
  196.     }
  197. ////////////////////
  198. void KH_Paint::bar3d(int l, int t, int r, int b, int d, int top)
  199.     {
  200.     int poly[] = { l,t, r,t, r,b, l,b, l,t };
  201.     fillpoly(5, poly);
  202.     if(b >= t)
  203.         {
  204.         if(top)
  205.             {
  206.             int poly2[] = { r,t, r+d,t-d, l+d,t-d, l,t, r,t };
  207.             drawpoly(5, poly2);
  208.             }
  209.         int poly1[] = { r,t, r,b, r+d,b-d, r+d,t-d, r,t };
  210.         drawpoly(5, poly1);
  211.         }
  212.     else
  213.         {
  214.         if(top)
  215.             {
  216.             int poly2[] = { r,b, r+d,b-d, l+d,b-d, l,b, r,b };
  217.             drawpoly(5, poly2);
  218.             }
  219.         int poly1[] = { r,t, r,b, r+d,b-d, r+d,t-d, r,t };
  220.         drawpoly(5, poly1);
  221.         }
  222.     }
  223. ///////////////////// Common DOS/Windows part of the DEMO ///////////////////
  224. //////////// This example draws "bird" and calendar for 1995 (1996) /////////
  225. /*
  226. #include <string.h>
  227.  
  228. class Demo : public KH_Paint
  229.     {
  230.     public:
  231.         void demo();
  232.         void bird();
  233.         void dn(int, int);
  234.         void wing(int, int);
  235.         void pero(int, int, int);
  236.         void calendar(int left, int top, int year, bool visocose);
  237.         int mon(int x, int y, char* mon, int first_day, int num_of_days);
  238.     };
  239. ///////////////////
  240. void Demo::demo()
  241.     {
  242.     set_add_zoom(1,1);          // For all picture
  243.  
  244.  
  245.     setfillstyle(SOLID_FILL, WHITE);
  246.     set_fill(ON);
  247.     set_zoom(0.1, 0.05);       // To the next "zoom" call
  248.     rectangle(0, 0, 5000, 10000);
  249.  
  250.     set_scroll(800, 500);
  251.     bird();
  252.     set_scroll(0, 500);
  253.     set_zoom(0.35, 0.33);
  254.  
  255.     setcolor(BLACK);
  256.     load("trip.chr");
  257. // Parameters: left, top, year, 0 if not visocose (1980 was)
  258.     calendar(80, 120, 1995, 0);
  259.  
  260.     char s[] = { "  KNOW-HOW.GRAPHICS" };
  261.     load("lcom.chr");
  262.     set_scroll(0, 0);
  263.     set_zoom(1, 1);
  264.     setcolor(GREEN);
  265.     for(int i = 0; i < strlen(s); i++)
  266.         {
  267.         rotate(loc(250, 250), 180 - 180 / strlen(s) * i);
  268.         draw_char(480, 250, s[i], 0);
  269.         }
  270.     rotate(0,0,0);
  271.     }
  272. /////////////////
  273. void Demo::calendar(int l, int t, int year, bool v)
  274.     {
  275.     int day = 7;              // for 1995
  276.     if(year == 1996)          // add cone for other years or use some
  277.         day = 1;              // calculations
  278.     int x = l;
  279.     int y = t + 15;
  280.     for(int i = 1; i <= 3; i++)
  281.         {
  282.         outtextxy(x, y, "Mo");
  283.         y += 30;
  284.         outtextxy(x, y, "Tu");
  285.         y += 30;
  286.         outtextxy(x, y, "We");
  287.         y += 30;
  288.         outtextxy(x, y, "Th");
  289.         y += 30;
  290.         outtextxy(x, y, "Fr");
  291.         y += 30;
  292.         outtextxy(x, y, "Sa");
  293.         y += 30;
  294.         outtextxy(x, y, "Su");
  295.  
  296.         y += 100;
  297.         }
  298.     x = l + 100;
  299.     y = t - 30;
  300.     day = mon(x, y, "January", day, 31);
  301.     int dv = v == 0 ? 28 : 29;
  302.  
  303.     day = mon(x + 340, y, "February", day, dv);
  304.     day = mon(x + 680, y, "March", day, 31);
  305.     day = mon(x + 1020, y, "April", day, 30);
  306.     day = mon(x, y +280, "May", day, 31);
  307.     day = mon(x + 340, y + 280, "June", day, 30);
  308.     day = mon(x + 680, y + 280, "July", day, 31);
  309.     day = mon(x + 1020, y + 280, "August", day, 31);
  310.     day = mon(x, y + 560, "September", day, 30);
  311.     day = mon(x + 340, y + 560, "October", day, 31);
  312.     day = mon(x + 680, y + 560, "November", day, 30);
  313.     day = mon(x + 1020, y + 560, "December", day, 31);
  314.     }
  315. //////////////
  316. int Demo::mon(int x1, int y1, char* head, int start, int num)
  317.     {
  318.     set_fill(ON);
  319.     setfillstyle(SOLID_FILL, WHITE);
  320.     outtextxy(x1 + 60, y1, head);
  321.     int days = 1;
  322.     int ln = start;
  323.     y1 += 15;
  324.  
  325.     char s[10];
  326.     while(1)
  327.         {
  328.         if(days > num)
  329.             return ln;
  330.         if(ln > 7)
  331.             {
  332.             x1 += 50;
  333.             ln = 1;
  334.             continue;
  335.             }
  336.         outtextxy(x1, y1 + 30 * ln, itoa(days, s, 10));
  337.         days++;
  338.         ln++;
  339.         }
  340.     }
  341. ////////////////
  342. void Demo::bird()
  343.     {
  344.     set_zoom(0.1, 0.05);       // To the next "zoom" call
  345.     setlinestyle(1, SOLID_LINE);
  346.     setcolor(BLUE);
  347.     setfillstyle(SOLID_FILL, YELLOW);
  348.  
  349.     int dy = 30;
  350.     for(int i = 1; i <= 12; i++)
  351.         {
  352.         ellipse(1600, 800 + 2 * dy * i, 0, 360, dy * i, dy * i);
  353.         ellipse(1600, 2300 - 2 * dy * i, 0, 360, dy * i, dy * i);
  354.         }
  355.     dn(1800, 1800);
  356.     wing(1900, 1400);
  357.     set_mirror(1600);
  358.     dn(1800, 1800);
  359.     wing(1900, 1400);
  360.     set_mirror(0);
  361.  
  362.     rotate(loc(0, 0), 0);
  363.     ellipse(1600, 700, 0, 360, 360, 360);
  364.     for(i = 0; i <= 60; i++)
  365.         {
  366.         pero(1800, 700, 1);
  367.         rotate(loc(1600, 700), 6 * i);
  368.         }
  369.     rotate(loc(1600, 2000), -90);
  370.     pero(1600, 2000, 7);
  371.     rotate(loc(1600, 2000), -100);
  372.     pero(1600, 2000, 5);
  373.     rotate(loc(1600, 2000), -80);
  374.     pero(1600, 2000, 5);
  375.  
  376.     rotate(loc(0, 0), 0);
  377.  
  378.     ellipse(1700, 600, 0, 360, 60, 130);
  379.     ellipse(1500, 600, 0, 360, 60, 130);
  380.     ellipse(1700, 600, 0, 360, 60, 60);
  381.     ellipse(1500, 600, 0, 360, 60, 60);
  382.     ellipse(1600, 800, 0, 360, 20, 90);
  383.     }
  384. //////////////////
  385. void Demo::wing(int xwin, int ywin)
  386.     {
  387.     for(int i = 0; i <= 10; i++)
  388.         {
  389.         rotate(xwin, ywin, 20 * i - 120);
  390.         pero(xwin, ywin, i / 2);
  391.         }
  392.     set_zoom(0.1, 0.05);
  393.     }
  394. /////////////////
  395. void Demo::dn(int xdn, int ydn)
  396.     {
  397.     rotate(xdn + 40, ydn + 40, -180);
  398.     for(int i = 0; i <= 19; i++)
  399.         {
  400.         rotate(xdn + 40, ydn + 40, 20 * i);
  401.         pero(xdn, ydn, 1);
  402.         }
  403.     }
  404. /////////////////
  405. void Demo::pero(int x, int y, int sz)
  406.     {
  407.     int x1 = x + 40 * sz;
  408.     int x2 = x + 100 * sz;
  409.     int x3 = x + 230 * sz;
  410.     int x4 = x + 300 * sz;
  411.     int y1 = y - 20 * sz;
  412.     int y2 = y - 30 * sz;
  413.     int y3 = y + 20 * sz;
  414.     int y4 = y + 30 * sz;
  415.     int points[] = {  x1, y, x2, y1, x3, y2, x4, y, x3, y4, x2, y3, x1, y };
  416.     fillpoly(7, points);
  417.     line(x, y, x + 300, y);
  418.     }
  419. ////////////////////// End of common DOS / Windows part /////////////////////
  420. //////////////////////////////// BGI DOS Demo ///////////////////////////////
  421.  
  422. void main()
  423.     {
  424.     int gdriver = DETECT, gmode;
  425.     initgraph(&gdriver, &gmode, "");
  426.  
  427.     Demo* paint = new Demo();
  428.     paint->demo();
  429.  
  430.     delete paint;
  431.     closegraph();
  432.     }
  433. */
  434. ////////////////////////////// GDI Windows Demo /////////////////////////////
  435. // This example demonstrates output to OWL window. If necessary,
  436. // any other Windows library could be used.
  437. /*
  438. #include <owl.h>
  439.  
  440. class TMyApp : public TApplication
  441. {
  442. public:
  443.   TMyApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  444.     LPSTR lpCmdLine, int nCmdShow)
  445.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  446.   virtual void InitMainWindow();
  447. };
  448.  
  449. _CLASSDEF(TMyWindow)
  450. class TMyWindow : public TWindow, public Demo
  451. {
  452. public:
  453.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  454.     : TWindow(AParent, ATitle), Demo() { DC = GetDC(HWindow); }
  455.   virtual void WMLButtonDown(RTMessage Msg)
  456.     = [WM_FIRST + WM_LBUTTONDOWN];
  457. };
  458.  
  459. void TMyWindow::WMLButtonDown(RTMessage)
  460.     {
  461.     DC = GetDC(HWindow);
  462.     demo();
  463.     }
  464.  
  465. void TMyApp::InitMainWindow()
  466. {
  467.   MainWindow = new TMyWindow(NULL, Name);
  468. }
  469.  
  470. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  471.   LPSTR lpCmdLine, int nCmdShow)
  472. {
  473.   TMyApp MyApp("Sample KNOW-HOW.GRAPHICS Program", hInstance, hPrevInstance,
  474.                lpCmdLine, nCmdShow);
  475.   MyApp.Run();
  476.   return MyApp.Status;
  477. }
  478.  
  479. */